<template>
    <v-app>
        <router-view></router-view>
        <Snackbar/>
    </v-app>
</template>

<script>
    import {mapGetters} from "vuex"
    import Snackbar from "@/components/Snackbar.vue";
    import {RefreshRoutes} from "@/util/dynamic";

    export default {
        name: "App",
        components: {
            Snackbar,
        },
        computed: {
            ...mapGetters("app", ["isMobile"]),
            ...mapGetters(["user"])
        },

        created() {
            console.log("App Created");
            this.$store.commit("app/setMobile", this._isMobile())
            // if(this.isMobile) {
            //   // this.$msg.info("mobile")
            //   console.info("mobile")
            //   this._localStorage()
            // } else {
            //   console.info("pc")
            //   // this.$msg.info("pc")
            //   this._sessionStorage()
            // }
            this._sessionStorage()
            // this._localStorage()
            setTimeout(() => {
                if (this.$store.getters.role) {
                    RefreshRoutes(this, this.$store.getters.role).then(() => {
                        console.log("无需登录");
                    });
                } else {
                    // console.log(this.$route.matched)
                    if(this.$route.matched[0].path !== "/login") {
                        this.$router.replace("/login");
                    }
                }
            }, 1000);

        },
        methods: {
            _isMobile() {
                // console.debug(navigator.userAgent)
                let flag = navigator.userAgent.match(
                    /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
                );
                return flag != null;
            },
            _sessionStorage() {
                if (window.sessionStorage.getItem("store")) {
                    this.$store.replaceState(
                        Object.assign(
                            {},
                            this.$store.state,
                            JSON.parse(window.sessionStorage.getItem("store"))
                        )
                    );
                    // window.sessionStorage.clear();
                }
                window.addEventListener("beforeunload", () => {
                    console.log("保存vuex至sessionStorage")
                    window.sessionStorage.clear()
                    window.sessionStorage.setItem("store", JSON.stringify(this.$store.state));
                    window.sessionStorage.setItem("token", JSON.stringify(this.$store.state.token))
                });
            },
            _localStorage() {
                if (window.localStorage.getItem("store")) {
                    this.$store.replaceState(
                        Object.assign(
                            {},
                            this.$store.state,
                            JSON.parse(window.localStorage.getItem("store"))
                        )
                    );
                    // window.localStorage.clear();
                }
                window.addEventListener("beforeunload", () => {
                    console.log("保存vuex至localStorage")
                    window.localStorage.clear()
                    window.localStorage.setItem("store", JSON.stringify(this.$store.state));
                });
            },
        },
    };
</script>
